package com.elock.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.JsonUtils;
import com.elock.base.core.util.WebUtils;
import com.elock.service.MemberService;
import com.elock.service.WarningMessageProcessService;

/**
 * Controller - 警报
 * @author 仁德
 */
@Controller("warningMessageProcessController")
@RequestMapping("/warning_message_process")
public class WarningMessageProcessController extends BaseController {

	@Resource(name = "warningMessageProcessServiceImpl")
	private WarningMessageProcessService warningMessageProcessService;
	@Resource(name = "memberServiceImpl")
	private MemberService memberService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list_tb", method = RequestMethod.GET)
	public String list_tb(ModelMap model) {

		return "/warning_message_process/list_tb";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Integer flag, Pageable pageable, ModelMap model) {

		model.addAttribute("flag", flag);
		return "/warning_message_process/list";
	}

	@RequestMapping(value = "/select_warning_list", method = RequestMethod.GET)
	public String select_warning_list(Integer flag, Pageable pageable, ModelMap model) {
		model.addAttribute("flag", flag);
		return "/warning_message_process/select_warning_list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg list_data(String terminalNum, String elockNum,
			String carNum, Integer type, Integer flag, Long oid, String version,
			Pageable pageable, ModelMap model) {
//		List<Filter> fis = new ArrayList<Filter>();
//		if (flag != null && flag == 1) { // 1只查未处理
//			fis.add(Filter.eq("isProcess", 0));
//		}
//		if (terminalNum != null) {
//			fis.add(Filter.like("terminalNum", "%" + terminalNum + "%"));
//		}
//		if (elockNum != null) {
//			fis.add(Filter.like("elockNum", "%" + elockNum + "%"));
//		}
//		Page<WarningMessageProcess> page = warningMessageProcessService.findPage(fis,
//				null,
//				pageable);
		Page<Map<String, Object>> page = warningMessageProcessService
				.findPage(terminalNum, elockNum, carNum, flag, version, pageable);
		String jsonPage = JsonUtils.toJson(page);

		return success(jsonPage);
	}

	@RequestMapping(value = "/handle", method = RequestMethod.POST)
	public @ResponseBody ResultMsg handle(Long[] ids) {

		if (ids == null || ids.length == 0) {
			return error("请选择需要处理的报警信息");
		}
		warningMessageProcessService.handle(ids);

		return success();
	}

	@RequestMapping(value = "/getNoProcess", method = RequestMethod.POST)
	public @ResponseBody ResultMsg getNoProcess() {

		String sql = "select count(1) from warning_message_process where is_process = 0 and company_info_id=?";
		int count = warningMessageProcessService.getDaoCenter().getNativeDao().findInt(
				sql, new Object[] { WebUtils.getCurrentCompanyInfoId() });
		return success().addObjX(count);
	}
}
